let app = getApp();
var siteinfo = require('../../siteinfo.js');
Page({
  data: {
    list: [], //放置返回数据的数组
    no_data: false, //“没有数据”的变量，默认false，隐藏
    reachLoading: true, //"上拉加载"的变量，默认false，隐藏
    current: 1, //第几页,第几次加载
    number: 12, //需要返回数据的个数
    floorstatus: false, 
  },
 
  onLoad: function() {
  },

  onLoad: function() {
    // 获取消息列表
    this.getMsgList();
  },
  //已读操作
  ready:function(e){
    var _this = this
    var list = _this.data.list;
    var id= e.currentTarget.dataset.id
    app._getLogin(siteinfo.updateStateApi, { id: id}, function (result) {
      //未读消息条数
      app._getLogin(siteinfo.unreadNumApi, { type: 0 }, function (result) {
        var unreadNum = result.data
        if (unreadNum != '' && unreadNum != 0) {
          wx.setStorageSync('unreadNum', unreadNum);
        }
        for (var i = 0; i < list.length; i++) {
          if (id == list[i].id) {
            list[i].state = 2
          }
        }
        _this.setData({
          list: list
        })
      });
    });
  },

  /**
   * 获取消息列表
   */
  getMsgList: function() {
    let _this = this;
    _this.setData({
      current: 1,
      no_data: false
    });
    app._getLogin(siteinfo.msgListApi, {
      type: 0,
      state: '',
      number: _this.data.number,
      current: _this.data.current
    }, function (result) {
      var res = result.data.records
      _this.setData({
        list: res
      });
      if ((result.data.current * result.data.size) >= result.data.total) {
        _this.setData({
          no_data: true
        });
      }
      wx.stopPullDownRefresh();
    })
    // app._get(siteinfo.msgListApi, {
    //   number: _this.data.number,
    //   current: _this.data.current
    // }, function(result) {
    //   //以/n把字符串进行拆分
    //   var list = result.data.page.records;
    //   for (var i = 0; i < list.length; i++) {
    //     list[i].content = list[i].content.split('/n')
    //   }
    //   _this.setData({
    //     list: result.data.page.records,
    //     msgCount: result.data.msgCount
    //   });
    //   if ((result.data.page.current * result.data.page.size) >= result.data.page.total) {
    //     _this.setData({
    //       no_data: true
    //     });
    //   }
    //   wx.stopPullDownRefresh();
    // });
  },
  /**
   * 获取消息
   */
  getMsg: function(e) {
    let _this = this;
    let formId = e.detail.formId;
//    console.log("formId", formId);
    if (formId == "the formId is a mock one") {
      app.showError("请在真机上操作");
    } else {
      app._post_form('msg/getMsg', {
        formId
      }, function(result) {
        app.showSuccess(result.msg, function() {
          _this.setData({
            msgCount: result.data
          })
        });
      });
    }
  },

  onPullDownRefresh: function(e) {
    this.getMsgList()
  },

  onReachBottom: function() {
    this.searchScrollLower();
  },

  searchScrollLower: function() {
    let _this = this;
    if (_this.data.reachLoading && !_this.data.no_data) {
      _this.setData({
        current: _this.data.current + 1, //每次触发上拉事件，把pageNumber+1
      });
      wx.showLoading({
        title: '加载中',
      })
      //网络请求
      _this.reachAppendMsgList();
    }
  },
  /**
   * 上拉追加消息列表
   */
  reachAppendMsgList: function() {
    let _this = this;
    app._getLogin(siteinfo.msgListApi, {
      type: 0,
      state: '',
      number: _this.data.number,
      current: _this.data.current
    }, function(result) {
      var list = result.data.records;
      _this.setData({
        list: _this.data.list.concat(result.data.records)
      });
      if ((result.data.current * result.data.size) >= result.data.total) {
        _this.setData({
          no_data: true
        });
      }
      wx.hideLoading();
    });
  },
  /**
   * 返回顶部
   */
  goTop: function(e) { // 一键回到顶部
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },

  /**
   * 显示/隐藏 返回顶部按钮
   */
  onPageScroll: function(e) {
    this.setData({
      floorstatus: e.scrollTop > 200
    })
  },
});